package com.gigi.service.manual;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gigi.entity.GigiClassEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.enums.YesOrNoEnum;
import com.gigi.mapper.generated.GigiClassMapper;
import com.gigi.model.ClassDO;
import com.gigi.model.ListClassRequest;
import com.gigi.service.generated.GigiClassService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ClassManageService {

    private final GigiClassService gigiClassService;
    private final GigiClassMapper gigiClassMapper;

    public ClassManageService(GigiClassService gigiClassService, GigiClassMapper gigiClassMapper) {
        this.gigiClassService = gigiClassService;
        this.gigiClassMapper = gigiClassMapper;
    }

    /**
     * 保存接口
     *
     * @param request
     * @return
     */
    public Response save(ClassDO request) {

        // 新增
        if (ObjectUtil.isNull(request.getId())) {

            // 判断班级是否已存在
            LambdaQueryWrapper<GigiClassEntity> wrapper = new LambdaQueryWrapper<GigiClassEntity>()
                    .eq(GigiClassEntity::getFlag, YesOrNoEnum.YES.getCode())
                    .eq(GigiClassEntity::getName, request.getName());
            if (gigiClassService.count(wrapper) > 0) {
                return Response.buildFailure(ErrorCodeEnum.CLASS_EXIST.getErrCode(), ErrorCodeEnum.CLASS_EXIST.getErrDesc());
            }

            GigiClassEntity target = buildTarget(request);
            boolean isSuccess = gigiClassService.save(target);
            if (!isSuccess) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }

            // 修改
        } else {

            GigiClassEntity oldModel = gigiClassService.getById(request.getId());
            if (Objects.isNull(oldModel)) {
                return Response.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
            }

            GigiClassEntity target = buildTarget(request);
            target.setId(oldModel.getId());
            boolean isSuccess = gigiClassService.updateById(target);
            if (!isSuccess) {
                return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

    /**
     * 创建target对象公共方法
     *
     * @param request
     * @return
     */
    private GigiClassEntity buildTarget(ClassDO request) {
        GigiClassEntity target = new GigiClassEntity();
        target.setCount(request.getCount());
        target.setName(request.getName());
        target.setFlag(request.getFlag());
        return target;
    }


    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<ClassDO> list(ListClassRequest request) {
        if (request.getPageNum() < 1) {
            request.setPageNum(1);
        }
        if (request.getPageSize() < 1) {
            request.setPageSize(10);
        }

        List<GigiClassEntity> classEntityList;
        int total;

        LambdaQueryWrapper<GigiClassEntity> wrapper = new LambdaQueryWrapper<GigiClassEntity>()
                .like(StrUtil.isNotBlank(request.getName()), GigiClassEntity::getName, request.getName())
                .eq(Objects.nonNull(request.getFlag()), GigiClassEntity::getFlag, request.getFlag())
                .orderByDesc(GigiClassEntity::getId);

        if (YesOrNoEnum.YES.getCode().equals(request.getIfByPage())) {
            Page<GigiClassEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
            Page<GigiClassEntity> classEntityPage = gigiClassMapper.selectPage(page, wrapper);
            classEntityList = classEntityPage.getRecords();
            total = (int) classEntityPage.getTotal();
        } else {
            classEntityList = gigiClassService.list(wrapper);
            total = classEntityList.size();
        }

        List<ClassDO> classDOS = null;
        if (CollUtil.isNotEmpty(classEntityList)) {
            classDOS = classEntityList.stream().map(e -> {
                ClassDO vo = new ClassDO();
                vo.setId(e.getId());
                vo.setName(e.getName());
                vo.setCount(e.getCount());
                vo.setFlag(e.getFlag());
                return vo;
            }).collect(Collectors.toList());
        }
        return MultiResponse.of(classDOS, total);
    }


}
